import { Module } from "vuex";
import { RootState } from "../types";
import { ISongItem } from "@/types/song-item";
import { PLAY_MODE, PLAY_STATUS } from "@/constant/play-mode";
import { DEFAULT_SONG_INFO } from "@/constant/song";
import { randomInteger } from "@/utils/random";

export interface PlayState {
    currentPlayingSong: ISongItem | null;
    playMode: PLAY_MODE;
    readyList: Array<ISongItem>;
    volume: number;
    playingStatus: PLAY_STATUS;
}

const state: PlayState = {
    currentPlayingSong: DEFAULT_SONG_INFO, // 正在播放的歌曲信息
    playMode: PLAY_MODE.SEQUENTIAL_PLAY,
    readyList: [],
    volume: 20,
    playingStatus: PLAY_STATUS.PAUSE, // 默认暂停状态
};

/**
 * 播放器播放信息：
 * 1. 当前播放歌曲信息
 * 2. 音量
 * 3. 歌词
 * 4. 循环模式等
 */
const playModule: Module<PlayState, RootState> = {
    namespaced: true, // 开启命名空间
    state,
    mutations: {
        SET_CURRENT_PLAYING_SONG: (state: any, song: ISongItem) => {
            state.currentPlayingSong = song;
            if (
                state.readyList.findIndex((v: ISongItem) => v.id == song.id) ==
                -1
            ) {
                state.readyList.push(song);
            }
        },
        SET_CURRENT_PLAY_MODE: (state: any, mode: ISongItem) => {
            state.playMode = mode;
        },
        Batch_ADD_2_READY_LIST(state: any, songs) {
            state.readyList.push(...songs);
        },
        CLEAR_READY_LIST(state: any) {
            state.readyList.splice(0, state.readyList.length);
        },
        SET_VOLUME(state: any, volume: number) {
            state.volume = volume;
        },
        SET_PLAYING_STATUS(state: any, status: PLAY_STATUS) {
            state.playingStatus = status;
        },
    },
    getters: {
        isPlaying: (state) => state.playingStatus == PLAY_STATUS.PLAYING,
    },
    actions: {
        updatePlayingInfo({ commit }, song: PlayState) {
            commit("SET_CURRENT_PLAYING_SONG", song);
        },
        updatePlayMode({ commit }, mode: PLAY_MODE) {
            commit("SET_CURRENT_PLAY_MODE", mode);
        },
        // 批量添加到待播放列表
        add2ReadyList({ commit, state }, songs: Array<ISongItem>) {
            if (Array.isArray(songs) && songs.length > 0) {
                const filteredSongs = songs.filter((v: ISongItem) => {
                    const index = state.readyList.findIndex(
                        (r: ISongItem) => r.id === v.id
                    );
                    return index == -1;
                });
                commit("Batch_ADD_2_READY_LIST", filteredSongs);
            }
        },
        // 竞选需要播放的歌曲
        campaign4Song({ commit, state }) {
            const randomNum = randomInteger(0, state.readyList.length - 1);
            const readySong = state.readyList[randomNum];
            commit("SET_CURRENT_PLAYING_SONG", readySong);
        },
        // 保存音量设置
        updateVolume({ commit }, volume) {
            commit("SET_VOLUME", volume);
        },
        // 更新播放状态
        updatePlayingStatus({ commit }, status: PLAY_STATUS) {
            commit("SET_PLAYING_STATUS", status);
        },
        // 清空播放列表
        clearReadyList({ commit }) {
            commit("CLEAR_READY_LIST");
        },
    },
};

export default playModule;
